package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspqceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspqceServiceImpl implements WorkspqceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    //获取当前日期
   private LocalDate today = LocalDate.now();
   private LocalDateTime begin = LocalDateTime.of(today, LocalTime.MIN);
   private LocalDateTime end = LocalDateTime.of(today, LocalTime.MAX);

   //修改起始日期,截至日期
    public void setBeginAndEnd(LocalDate beginDate,LocalDate endDate){
        this.begin = LocalDateTime.of(beginDate, LocalTime.MIN);
        this.end= LocalDateTime.of(endDate, LocalTime.MAX);
    }
    /**
     * 今日数据查询
     * @return
     */
    @Override
    public BusinessDataVO getBusinessDate() {
        log.info("开始时间{},截至时间{}",begin,end);
        //查询营业额
        Double turnover = orderMapper.getDayMoney(begin, end, Orders.COMPLETED);
        turnover=turnover==null?0.0:turnover;

        //获取订单总数
        Map map=new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        Integer totalOrders = orderMapper.ordersStatistics(map);
        totalOrders=totalOrders==null?0:totalOrders;

        //获取有效订单数
        map.put("status",Orders.COMPLETED);
        Integer validOrderCount =orderMapper.ordersStatistics(map);
        validOrderCount=validOrderCount==null?0:validOrderCount;

        //计算订单完成率
        Double  orderCompletionRate;
        orderCompletionRate=totalOrders==0?0.0:validOrderCount*1.0/totalOrders;

        //计算平均客单价格
        Double unitPrice;
        unitPrice=validOrderCount==0?0.0:turnover*1.0/validOrderCount;

        //新增用户数
        map.remove("status");
        Integer newUsers = userMapper.getUserCount(map);

        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
        return businessDataVO;
    }

    /**
     * 订单数据查询
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        log.info("开始时间{},截至时间{}",begin,end);
        Map map=new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //全部订单
        Integer allOrders=orderMapper.ordersStatistics(map);
        allOrders=allOrders==null?0:allOrders;

        //待接单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.ordersStatistics(map);
        waitingOrders=waitingOrders==null?0:waitingOrders;

        //待派送数量
        map.put("status",Orders.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders=orderMapper.ordersStatistics(map);
        deliveredOrders=deliveredOrders==null?0:deliveredOrders;

        ////已完成数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders=orderMapper.ordersStatistics(map);
        completedOrders=completedOrders==null?0:completedOrders;

        ////已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders=orderMapper.ordersStatistics(map);
        cancelledOrders=cancelledOrders==null?0:cancelledOrders;

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();

    }

    /**
     * 菜品预览
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        Map map=new HashMap();
        // 已启售数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold=dishMapper.getStatusDishCount(map);
        sold=sold==null?0:sold;

        // 已停售数量
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued=dishMapper.getStatusDishCount(map);
        discontinued=discontinued==null?0:discontinued;

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 套餐预览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Map map=new HashMap();
        // 已启售数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold=setmealMapper.getStatusSetmealCount(map);
        sold=sold==null?0:sold;

        // 已停售数量
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued=setmealMapper.getStatusSetmealCount(map);
        discontinued=discontinued==null?0:discontinued;

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 今日数据--导出数据使用的
     * @return
     */
    public BusinessDataVO getBusinessDate2(LocalDate beginDate,LocalDate enddDate) {
        setBeginAndEnd(beginDate,enddDate);
        log.info("开始时间{},截至时间{}",begin,end);
        //查询营业额
        Double turnover = orderMapper.getDayMoney(begin, end, Orders.COMPLETED);
        turnover=turnover==null?0.0:turnover;

        //获取订单总数
        Map map=new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        Integer totalOrders = orderMapper.ordersStatistics(map);
        totalOrders=totalOrders==null?0:totalOrders;

        //获取有效订单数
        map.put("status",Orders.COMPLETED);
        Integer validOrderCount =orderMapper.ordersStatistics(map);
        validOrderCount=validOrderCount==null?0:validOrderCount;

        //计算订单完成率
        Double  orderCompletionRate;
        orderCompletionRate=totalOrders==0?0.0:validOrderCount*1.0/totalOrders;

        //计算平均客单价格
        Double unitPrice;
        unitPrice=validOrderCount==0?0.0:turnover*1.0/validOrderCount;

        //新增用户数
        map.remove("status");
        Integer newUsers = userMapper.getUserCount(map);

        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
        return businessDataVO;
    }

}
